// pages/mine/posterList/index.js

var service = require('../../../service/port')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0, //当前选中的图片
    posterList: [],
    pageSize: 10,
    pageNum: 1,
    totalCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = {
      queryRequest: {
        pageSize: this.data.pageSize,
        pageNum: this.data.pageNum
      }
    }

    service.getPosterList(data, (res) => {
      if (res.code == '00000') {
        this.setData({
          posterList: res.data.rows || [],
          totalCount: res.data.total
        })
      }
    })

  },

  onImageTap(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })
  },

  scrolltolower() {

    let length = this.data.posterList.length
    let totalCount = this.data.totalCount

    if (length >= totalCount) return;

    let pageNum = this.data.pageNum
    this.setData({
      pageNum: pageNum++
    })
    let data = {
      queryRequest: {
        pageSize: this.data.pageSize,
        pageNum: this.data.pageNum
      }
    }
    // title url 
    service.getPosterList(data, (res) => {
      if (res.code == '00000') {
        let list = this.data.posterList
        list = list.concat(res.data.rows || [])
        this.setData({
          posterList: list
        })
      }
    })
  },
  //生成海报
  createPoster () {
    let param = this.data.posterList[this.data.activeIndex]
    let jsonParam = JSON.stringify(param)
    wx.navigateTo({
      url: '/pages/mine/createPoster/index?param=' + jsonParam,
    })
  }
})